package cn.ex.basic.service.impl;

import cn.ex.basic.mapper.BasicMapper;
import cn.ex.basic.query.BasicQuery;
import cn.ex.basic.service.IBasicService;
import cn.ex.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: ZeWangmu
 * @Date: 2022/06/08/15:38
 * @Description:
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BasicServiceImpl<T> implements IBasicService<T> {
    @Autowired
    private BasicMapper<T> mapper;
    @Override
    public List<T> findAll() {
        return mapper.loadAll();
    }

    @Override
    public T findById(Long id) {
        return mapper.loadById(id);
    }

    @Override
    @Transactional
    public void save(T t) {
        mapper.save(t);
    }

    @Override
    @Transactional
    public void removeById(Long id) {
        mapper.remove(id);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    @Transactional
    public void batchRemove(Long[] ids) {
        mapper.batchRemove(ids);
    }

    /**
     *
     * @param basicQuery
     * @return
     */
    @Override
    public PageList<T> queryPage(BasicQuery basicQuery) {
        //先查询数据总条数
        Long total = mapper.queryCount(basicQuery);
        if(total==null ||total.intValue()<1){
            return new PageList<>();
        }
        List<T> query = mapper.queryData(basicQuery);
        return new PageList<>(total,query);
    }


}
